/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Database.Koneksi;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Satellite
 */
public class Member {
    static char nameFalse[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
    private String id;
    private String user;
    private String pass;
    private String nama;
    private String alamat;
    private String ttl;
    private String hp;
    private String norek;
    private String email;

    public String getAlamat() {
        return alamat;
    }

    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        if (isEmailValid(email) == true) {
            this.email = email;
        } else {
            this.email = null;
        }
    }

    public String getHp() {
        return hp;
    }

    public void setHp(String hp) {
        this.hp = hp;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        if (isUserNameValid(nama) == true) {
            this.nama = nama;
        } else {
            this.nama = null;
        }
    }

    public String getNorek() {
        return norek;
    }

    public void setNorek(String norek) {
        this.norek = norek;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getTtl() {
        return ttl;
    }

    public void setTtl(String ttl) {
        if (isBirthDayValid(ttl) == true) {
            this.ttl = ttl;
        } else {
            this.ttl = null;
        }
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
    
    

    public boolean isUserNameValid(String name) {
        char karakter;
        for (int i = 0; i < name.length(); i++) {
            karakter = name.charAt(i);
            for (int j = 0; j < nameFalse.length; j++) {
                if (karakter == nameFalse[j]) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean isEmailValid(String email) {
        char karakter;
        for (int i = 0; i < email.length(); i++) {
            karakter = email.charAt(i);
            if (karakter == '@') {
                return true;
            }
        }
        return false;
    }

    public boolean isBirthDayValid(String bday) {
        if (bday.length() != 10) {
            return false;
        }
        String day1 = null, day2 = null, month1 = null, month2 = null;
        char pembatas[] = new char[2];
        for (int i = 0; i < bday.length(); i++) {
            day1 = String.valueOf(bday.charAt(0));
            day2 = String.valueOf(bday.charAt(1));
            month1 = String.valueOf(bday.charAt(3));
            month2 = String.valueOf(bday.charAt(4));
            pembatas[0] = bday.charAt(2);
            pembatas[1] = bday.charAt(5);
        }
        int day = Integer.parseInt(day1 + day2);
        int month = Integer.parseInt(month1 + month2);
        if (day > 31) {
            return false;
        } else if (month > 12) {
            return false;
        } else if ((day > 31) && (month > 12)) {
            return false;
        } else if ((pembatas[0] != '-') && (pembatas[1] != '-')) {
            return false;
        }
        return true;
    }
    public  void signup(Model.Member mem ) throws SQLException{
        
        Connection con = Koneksi.open();
        Statement st = null;
        
        String id = mem.getId();
        String user = mem.getUser();
        String pass = mem.getPass();
        String name = mem.getNama();
        String almt = mem.getAlamat();
        String notlpn = mem.getHp();
        String rek  = mem.getNorek();
        String email = mem.getEmail();
       
        st = con.createStatement();

//        String sql = "insert into member (id_member,usename,password,nama,alamat ,no_rek,no_hp,email) values('"
//                + id + "-" + user + "-" + pass + "','"
//                + name + "','" + almt + "','" + notlpn + "','" + rek + "','" + email +  "')";

        String sql = "insert into member(id_member,username,password,nama,alamat,no_rek,no_hp,email) values "
                + "('"+id+"','"+user+"','"+pass+"','"+name+"','"+almt+"','"+norek+"','"+notlpn+"','"+email+"')";
        
        
        ResultSet rs = null;
        rs = st.executeQuery(sql);

        con.close();
    }
}